/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import org.openaecfm.model.structuralload.StructuralLoad;
import org.openaecfm.model.structuralload.SurfaceReinforcementArea;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class SurfaceReinforcementAreaIO extends
		AbstractEntityInstanceInputImpl<SurfaceReinforcementArea> implements
		IEntityInstanceOutput<SurfaceReinforcementArea> {

	private IEntityInstanceUtilOutput<StructuralLoad> utilOutput;
	private IEntityInstanceUtilInput<StructuralLoad> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<StructuralLoad> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<StructuralLoad> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(SurfaceReinforcementArea pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSURFACEREINFORCEMENTAREA", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set SurfaceReinforcement1
		if (pojo.getSurfaceReinforcement1() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter lp = new ListParameter();
			for (Double d : pojo.getSurfaceReinforcement1()) {
				lp.add(new RealParameter(d));
			}
			ei.addParameter(lp);
		}

		// Set SurfaceReinforcement2
		if (pojo.getSurfaceReinforcement2() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter lp = new ListParameter();
			for (Double d : pojo.getSurfaceReinforcement2()) {
				lp.add(new RealParameter(d));
			}
			ei.addParameter(lp);
		}

		// Set ShearReinforcement
		if (pojo.getShearReinforcement() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getShearReinforcement()));
		}

		return ei.toString();
	}

	@Override
	protected SurfaceReinforcementArea doCreateObject() {
		return new SurfaceReinforcementArea();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			SurfaceReinforcementArea pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SurfaceReinforcement1
		Parameter s1 = sei.getParameter(1);
		if (s1.getNodeType() == NodeType.LIST) {
			ListParameter lp = (ListParameter) s1;
			for (Parameter p : lp) {
				RealParameter r = (RealParameter) p;
				pojo.getSurfaceReinforcement1().add(r.toDouble());
			}
		}

		// Set SurfaceReinforcement2
		Parameter s2 = sei.getParameter(2);
		if (s2.getNodeType() == NodeType.LIST) {
			ListParameter lp = (ListParameter) s2;
			for (Parameter p : lp) {
				RealParameter r = (RealParameter) p;
				pojo.getSurfaceReinforcement2().add(r.toDouble());
			}
		}

		// Set ShearReinforcement
		Parameter sr = sei.getParameter(3);
		if (sr.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) sr;
			pojo.setShearReinforcement(r.toDouble());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SurfaceReinforcementArea pojo, IAppDataManager appDataManager) {
		return ;
	}
}
